import axios from 'axios';
import qs from 'qs';
const validateStatus = (status) => {
    return status >= 200 && status < 300;
};
axios.defaults.timeout = 60000;
axios.defaults.withCredentials = true; // 设置允许跨域设置cookie
axios.defaults.baseURL = '/apis';
axios.defaults.validateStatus = validateStatus;
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers['x-requested-with'] = 'XMLHttpRequest';
// http request 拦截器
axios.interceptors.request.use(
    (config) => {
        if (
            config.method === 'post' &&
            config.headers['Content-Type'] != 'multipart/form-data'
        ) {
            config.data = qs.stringify(config.data);
        }
        return config;
    },
    (err) => {
        return Promise.reject(err);
    }
);

// http request 拦截器
axios.interceptors.response.use(
    (resp) => {
        // if (resp.code !== 200) {
        //     return Promise.reject({
        //         strMsg: '接口错误'
        //     });
        // }
        // const { intCode, strMsg } = resp.data;
        // if (!validateStatus(intCode) && intCode !== undefined) {
        //     //返回错误状态
        //     return Promise.reject(strMsg)
        // }
        return resp;
    },
    (err) => {
        err = err || '网络连接错误';
        return Promise.reject(err);
    }
);

export default axios;
